let webpack = require("webpack");
let UglifyJsPlugin = require('uglifyjs-webpack-plugin');
let BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
//env:开发环境dev 生成环境：pro 本地Mock环境:local
let env=process.env.VUE_NODE_ENV;
//部署路径
let baseUrl=process.env.BASE_URL;
module.exports = {
    //具体配置可参考地址：https://cli.vuejs.org/config/#vue-config-js
    //webpack  配置
    // Project deployment base
    // By default we assume your app will be deployed at the root of a domain,
    // e.g. https://www.my-app.com/
    // If your app is deployed at a sub-path, you will need to specify that
    // sub-path here. For example, if your app is deployed at
    // https://www.foobar.com/my-app/
    // then change this to '/my-app/'
    // baseUrl: '/qd/mobile/',
    publicPath: baseUrl,

    // where to output built files
    outputDir: 'dist',

    // whether to use eslint-loader for lint on save.
    // valid values: true | false | 'error'
    // when set to 'error', lint errors will cause compilation to fail.
    lintOnSave: true,

    // use the full build with in-browser compiler?
    // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
    runtimeCompiler: false,

    // babel-loader skips `node_modules` deps by default.
    // explicitly transpile a dependency with this option.
    transpileDependencies: [],
    // generate sourceMap for production build?
    productionSourceMap: false,
    // CSS related options
    css: {
        // extract CSS in components into a single CSS file (only in production)
        // can also be an object of options to pass to extract-text-webpack-plugin
        extract: true,

        // enable CSS source maps?
        sourceMap: false,

        // pass custom options to pre-processor loaders. e.g. to pass options to
        // sass-loader, use { sass: { ... } }
        loaderOptions: {
            sass: {
                data: `@import "~@/style/common.less";`
            }
        },
        // Enable CSS modules for all css / pre-processor files.
        // This option does not affect *.vue files.
        // modules: false
    },

    // use thread-loader for babel & TS in production build
    // enabled by default if the machine has more than 1 cores
    parallel: require('os').cpus().length > 1,

    // options for the PWA plugin.
    // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
    pwa: {},
    //webpack  配置
    configureWebpack:config=>{
        config.externals={
            'vue':'Vue',
            'vue-router': 'VueRouter',
            'vuex': 'Vuex',
            'axios':'axios',
            "jquery":"jQuery",
            "mockjs":'Mock',
            'element-ui': 'ELEMENT'
        }
    },
    // tweak internal webpack configuration.
    // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
    chainWebpack:config => {
        //自动加载 jquery
        config.plugin('provide').use(webpack.ProvidePlugin, [{
            $: 'jquery',
            jquery: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        }]);
        //压缩
        if(env=="production"){
            console.info(env);
            config.plugin('uglifyJs').use(UglifyJsPlugin,[{
                uglifyOptions: {
                    compress: {
                        drop_debugger: true,
                        drop_console: true
                    },
                }
            }]);
            //生成打包文件-报告
            config.plugin('webpack-report').use(BundleAnalyzerPlugin, [{
                analyzerMode: 'static',
            }]);
            config.plugin('chunkPlugin').use(webpack.optimize.LimitChunkCountPlugin,[{
                maxChunks:15, // 必须大于或等于 1
                minChunkSize: 10000
            }])
        }
    },
    devServer: {
        open: process.platform === 'darwin',
        host: '0.0.0.0',
        port: 10008,
        https: false,
        hotOnly: false,
        // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#configuring-proxy
        proxy: null, // string | Object
        before: app => {}
    },

    // options for 3rd party plugins
    pluginOptions: {
        // ...
    }
}